package com.peanut.baby.mvp;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.peanut.baby.AppConfig;
import com.peanut.baby.InitManager;
import com.peanut.baby.comm.BaseDialog;
import com.peanut.baby.mvp.bind.BindMobileActivity;
import com.peanut.baby.mvp.login.LoginActivity;
import com.peanut.baby.mvp.task.TaskActivity;
import com.peanut.devlibrary.BaseActivity;
import com.peanut.devlibrary.util.StringUtil;
import com.tencent.mars.xlog.Log;

/**
 * Created by anonymouslystudio on 2017/6/15.
 */

public class BaseMvpActivity extends BaseActivity{

    private Dialog apiDialog;
    private IntentFilter filter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

        filter =new IntentFilter();
        filter.addAction(AppConfig.BroadCastConfig.ACTION_TOKEN_EXPIRES);
        filter.addAction(AppConfig.BroadCastConfig.ACTION_LACK_POINT);
        filter.addAction(AppConfig.BroadCastConfig.ACTION_NETWORK_FAILED);
    }

    @Override
    protected void onResume() {
        super.onResume();

        registerReceiver(apiExceptionReceiver,filter);

    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(apiExceptionReceiver);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        filter = null;
        apiExceptionReceiver = null;

    }

    private BroadcastReceiver apiExceptionReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            dismissProgressDialog();
            if(apiDialog != null && apiDialog.isShowing()){
                return;
            }
            String action = intent.getAction();
            Log.d("apiExceptionReceiver","action:"+action);
            //按优先级判断
            if(intent.getAction().equals(AppConfig.BroadCastConfig.ACTION_TOKEN_EXPIRES)){
                apiDialog =new BaseDialog().showConfirmAlert(BaseMvpActivity.this, "您的访问凭证过期，需要重新登录~", "登录", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LoginActivity.startForResult(BaseMvpActivity.this);
                    }
                });
            }else if(intent.getAction().equals(AppConfig.BroadCastConfig.ACTION_LACK_POINT)){
                apiDialog =new BaseDialog().showConfirmAlert(BaseMvpActivity.this, "您的花生壳不足，马上赚取花生壳！", "确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (InitManager.getInstance().getUser() == null) {
                            LoginActivity.startForResult(BaseMvpActivity.this);
                            return;
                        }
                        if (StringUtil.isNullOrEmpty(InitManager.getInstance().getUser().mobile)) {
                            BindMobileActivity.startForResult(BaseMvpActivity.this);
                            return;
                        }

                        TaskActivity.start(BaseMvpActivity.this);
                    }
                });
            }else if(intent.getAction().equals(AppConfig.BroadCastConfig.ACTION_NEED_TOKEN)){
                apiDialog =new BaseDialog().showConfirmAlert(BaseMvpActivity.this, "用户访问凭证不存在，请绑定手机号后重新进入此页面", "确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BindMobileActivity.startForResult(BaseMvpActivity.this);
                    }
                });
            }else if(intent.getAction().equals(AppConfig.BroadCastConfig.ACTION_NETWORK_FAILED)){
                showToast("请求失败，请检查您的网络连接");
            }

        }
    };
}
